package com.atguigu.gmall.cart.exception.handle;

import lombok.extern.slf4j.Slf4j;
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundSetOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.Arrays;

@Component
@Slf4j
public class AsyncExceptionHandle implements AsyncUncaughtExceptionHandler {

    private  final static String KEY = "cart:async:exception";
    @Autowired
    StringRedisTemplate redisTemplate;
    /**
     * 将异常信息记录到redis
     * @param throwable
     * @param method
     * @param objects
     */
    @Override
    public void handleUncaughtException(Throwable throwable, Method method, Object... objects) {
     //将同步失败的用户id存储到redis数据中，以固定的值为key
            BoundSetOperations<String, String> setOps = redisTemplate.boundSetOps(KEY);
            setOps.add(objects[0].toString());//用户id不可重复,所以使用set
    }
}
